package com.example.mall.web.controller;

import com.example.mall.domain.Login;
import com.example.mall.dto.LoginDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.LoginQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.ILoginService;
import com.example.mall.util.AssertUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public ILoginService loginService;

    /**
     * 分页查询登录信息表信息
     * @param loginQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询登录信息表信息",notes = "分页查询登录信息表信息",httpMethod = "GET")
    public JSONResult queryPage(LoginQuery loginQuery){
        PageDTO<LoginDto> loginPageDTO = loginService.queryPage(loginQuery);
        return JSONResult.success("查询成功",loginPageDTO);
    }

    /**
     * 根据id查询登录信息表信息
     * @param id
     * @return
     */
    @GetMapping("query/{id}")
    @ApiOperation(value = "根据id查询登录信息表信息",notes = "根据id查询登录信息表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("id")Long id){
        //1.根据id查询用户信息
        if (id == null){
            throw new GlobalCustomerException(ErrorEnumCode.LOGINID_NOT_ID);
        }
        Login login = loginService.getById(id);
        return JSONResult.success("查询成功",login);
    }

    /**
     * 根据id查询登录信息表信息
     * @param id
     * @return
     */
    @GetMapping("queryLoginById/{id}")
    @ApiOperation(value = "根据id查询登录信息表信息",notes = "根据id查询登录信息表信息",httpMethod = "GET")
    public JSONResult queryLoginById(@PathVariable("id")Long id){
        //1.根据id查询用户信息
        if (id == null){
            throw new GlobalCustomerException(ErrorEnumCode.LOGINID_NOT_ID);
        }
        Login login = loginService.queryLoginById(id);
        return JSONResult.success("查询成功",login);
    }

    /**
     * 查询全部登录信息表信息
     * @param
     * @return
     */
    @GetMapping("queryLoginList")
    @ApiOperation(value = "查询全部登录信息表信息",notes = "查询全部登录信息表信息",httpMethod = "GET")
    public JSONResult queryLoginList(){
        List<Login> logins = loginService.queryLoginList();
        return JSONResult.success("查询成功",logins);
    }

    /**
     * 新增登录信息表
     * @param loginDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增登录信息表",notes = "新增登录信息表",httpMethod = "POST")
    public JSONResult insert(@RequestBody LoginDto loginDto){
        loginService.insert(loginDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改登录信息表信息
     * @param loginDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改登录信息表信息",notes = "修改登录信息表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody LoginDto loginDto){
        loginService.update(loginDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除登录信息表信息
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除登录信息表信息",notes = "删除登录信息表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("id") Long id) {
        loginService.delete(id);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除登录信息表信息
     * @param ids
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除登录信息表信息",notes = "批量删除登录信息表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> ids) {
        loginService.deleteBatch(ids);
        return JSONResult.success("批量删除成功");
    }

}
